Modification start date
[BattleCats.git] / Assets / EZ Camera Shake / Scripts / CameraShakeInstance.cs
blob1bc67dde03e4e17e81c6b64367288a6281ec9e27
1 using UnityEngine;
3 namespace EZCameraShake
5 public enum CameraShakeState { FadingIn, FadingOut, Sustained, Inactive }
7 public class CameraShakeInstance
9 /// <summary>
10 /// The intensity of the shake. It is recommended that you use ScaleMagnitude to alter the magnitude of a shake.
11 /// </summary>
12 public float Magnitude;
14 /// <summary>
15 /// Roughness of the shake. It is recommended that you use ScaleRoughness to alter the roughness of a shake.
16 /// </summary>
17 public float Roughness;
19 /// <summary>
20 /// How much influence this shake has over the local position axes of the camera.
21 /// </summary>
22 public Vector3 PositionInfluence;
24 /// <summary>
25 /// How much influence this shake has over the local rotation axes of the camera.
26 /// </summary>
27 public Vector3 RotationInfluence;
29 /// <summary>
30 /// Should this shake be removed from the CameraShakeInstance list when not active?
31 /// </summary>
32 public bool DeleteOnInactive = true;
35 float roughMod = 1, magnMod = 1;
36 float fadeOutDuration, fadeInDuration;
37 bool sustain;
38 float currentFadeTime;
39 float tick = 0;
40 Vector3 amt;
42 /// <summary>
43 /// Will create a new instance that will shake once and fade over the given number of seconds.
44 /// </summary>
45 /// <param name="magnitude">The intensity of the shake.</param>
46 /// <param name="fadeOutTime">How long, in seconds, to fade out the shake.</param>
47 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
48 public CameraShakeInstance(float magnitude, float roughness, float fadeInTime, float fadeOutTime)
50 this.Magnitude = magnitude;
51 fadeOutDuration = fadeOutTime;
52 fadeInDuration = fadeInTime;
53 this.Roughness = roughness;
54 if (fadeInTime > 0)
56 sustain = true;
57 currentFadeTime = 0;
59 else
61 sustain = false;
62 currentFadeTime = 1;
65 tick = Random.Range(-100, 100);
68 /// <summary>
69 /// Will create a new instance that will start a sustained shake.
70 /// </summary>
71 /// <param name="magnitude">The intensity of the shake.</param>
72 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
73 public CameraShakeInstance(float magnitude, float roughness)
75 this.Magnitude = magnitude;
76 this.Roughness = roughness;
77 sustain = true;
79 tick = Random.Range(-100, 100);
82 public Vector3 UpdateShake()
84 amt.x = Mathf.PerlinNoise(tick, 0) - 0.5f;
85 amt.y = Mathf.PerlinNoise(0, tick) - 0.5f;
86 amt.z = Mathf.PerlinNoise(tick, tick) - 0.5f;
88 if (fadeInDuration > 0 && sustain)
90 if (currentFadeTime < 1)
91 currentFadeTime += Time.deltaTime / fadeInDuration;
92 else if (fadeOutDuration > 0)
93 sustain = false;
96 if (!sustain)
97 currentFadeTime -= Time.deltaTime / fadeOutDuration;
99 if (sustain)
100 tick += Time.deltaTime * Roughness * roughMod;
101 else
102 tick += Time.deltaTime * Roughness * roughMod * currentFadeTime;
104 return amt * Magnitude * magnMod * currentFadeTime;
107 /// <summary>
108 /// Starts a fade out over the given number of seconds.
109 /// </summary>
110 /// <param name="fadeOutTime">The duration, in seconds, of the fade out.</param>
111 public void StartFadeOut(float fadeOutTime)
113 if (fadeOutTime == 0)
114 currentFadeTime = 0;
116 fadeOutDuration = fadeOutTime;
117 fadeInDuration = 0;
118 sustain = false;
121 /// <summary>
122 /// Starts a fade in over the given number of seconds.
123 /// </summary>
124 /// <param name="fadeInTime">The duration, in seconds, of the fade in.</param>
125 public void StartFadeIn(float fadeInTime)
127 if (fadeInTime == 0)
128 currentFadeTime = 1;
130 fadeInDuration = fadeInTime;
131 fadeOutDuration = 0;
132 sustain = true;
135 /// <summary>
136 /// Scales this shake's roughness while preserving the initial Roughness.
137 /// </summary>
138 public float ScaleRoughness
140 get { return roughMod; }
141 set { roughMod = value; }
144 /// <summary>
145 /// Scales this shake's magnitude while preserving the initial Magnitude.
146 /// </summary>
147 public float ScaleMagnitude
149 get { return magnMod; }
150 set { magnMod = value; }
153 /// <summary>
154 /// A normalized value (about 0 to about 1) that represents the current level of intensity.
155 /// </summary>
156 public float NormalizedFadeTime
157 { get { return currentFadeTime; } }
159 bool IsShaking
160 { get { return currentFadeTime > 0 || sustain; } }
162 bool IsFadingOut
163 { get { return !sustain && currentFadeTime > 0; } }
165 bool IsFadingIn
166 { get { return currentFadeTime < 1 && sustain && fadeInDuration > 0; } }
168 /// <summary>
169 /// Gets the current state of the shake.
170 /// </summary>
171 public CameraShakeState CurrentState
175 if (IsFadingIn)
176 return CameraShakeState.FadingIn;
177 else if (IsFadingOut)
178 return CameraShakeState.FadingOut;
179 else if (IsShaking)
180 return CameraShakeState.Sustained;
181 else
182 return CameraShakeState.Inactive;